import { GameName } from "./GameConfig";
import SDKUtil from "./SDK/SDKUtil";
import Utils from "./Utils";

const { ccclass, property } = cc._decorator;


@ccclass
export default class DataManager {

    protected static _instance: DataManager = null;
    public static get Instance(): DataManager {
        if (DataManager._instance == null) {
            DataManager._instance = new DataManager();
        }
        return DataManager._instance;
    }

    private _loginData: any = {};
    private _goodsData: any = [];
    private _isFirst: boolean = true;
    private _rankReTime: number = 0;

    setLoginData(data: any) {
        if (data) {
            this._loginData = data.player || data;
            if (data.nextWeekMonday) this._rankReTime = data.thisWeekMonday;
            console.log("设置登录数据成功", data)
            if (this._isFirst) {
                let now = this.getRankReTime();
                SDKUtil.Instance.postData(this.getMaxScore() + "", now);
                this._isFirst = false;
            }
        }
    }

    getRankReTime() {
        return this._rankReTime || Date.now();
    }

    setGoodsData(data: any) {
        if (cc.isValid(data)) {
            this._goodsData = data;
        }
    }

    getGoodsData() {
        return this._goodsData;
    }

    getNickName() {
        if (!this._loginData.name) return "玩家";
        return this._loginData.name;
    }


    getAvatarUrl() {
        return this._loginData.avatarUrl;
    }


    getLoginData() {
        return this._loginData;
    }

    getPlayerId() {
        if (!cc.isValid(this._loginData.id)) return "XIAOBAI";
        return this._loginData.id;
    }

    getOpenId() {
        if (!cc.isValid(this._loginData.openId)) return "0";
        return this._loginData.openId;
    }

    getIntegral() {
        if (!cc.isValid(this._loginData.integral)) {
            return 0;
        }
        return this._loginData.integral;
    }
    getMaxScore() {
        if (!cc.isValid(this._loginData.maxIntegral)) {
            return Utils.Instance.getStorageByUid(this.getPlayerId(), "GAME_MAX_SCORE", 0);
        }
        return this._loginData.maxIntegral;
    }

    getVideoNum() {
        return this._loginData.videoNum;
    }

    setVideoNum(count) {
        return this._loginData.videoNum += count;
    }

    getAccount() {
        return this._loginData.openId;
    }

    getEffectIsOff() {
        let EffectIsOff = (Utils.Instance.getStorageByUid(GameName, "EFFECT_STATE", 1) == 0);
        return EffectIsOff;
    }

    getBgmIsOff() {
        let BgmIsOff = (Utils.Instance.getStorageByUid(GameName, "BGM_STATE", 1) == 0);
        return BgmIsOff;
    }

    setEffectIsOff(state) {
        Utils.Instance.setStorageByUid(GameName, "EFFECT_STATE", state);
        cc.log("设置音效：", DataManager.Instance.getEffectIsOff())
    }

    setBgmIsOff(state) {
        Utils.Instance.setStorageByUid(GameName, "BGM_STATE", state);
        cc.log("设置BGM：", DataManager.Instance.getBgmIsOff())
    }

}
